#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Author          : ricsy
@File            : terminal_interactive.py
@Create Time     : 2025/6/18 03:46
@Last Modified   :
@Description     : 终端指令配置生成器
"""
from ipywidgets import widgets

from extensions.common import deep_merge
from extensions.interactive.base_interactive import BaseInteractive
from extensions.sphinx_terminal import TerminalDirective


class TerminalInteractive(BaseInteractive):
    DIRECTIVE_NAME = "terminal"
    CLASS_PREFIX = "terminal-preview"

    DEFAULT_OPTIONS = deep_merge(
        TerminalDirective.DEFAULT_OPTIONS,
        {"user": {"value": "root"}, "height": {"value": 152}},
    )

    CONTROL_LAYOUT_CONFIG = {
        "shell": {"width": "30%", "margin": "2px 0.5rem 2px 2px"},
        "platform": {"width": "30%", "margin": "2px 0.5rem 2px 2px"},
        "style": {"width": "30%", "margin": "2px 0.5rem 2px 2px"},
        "color": {"width": "30%", "margin": "2px 0.5rem 2px 2px"},
        "blink": {"width": "30%"},
        "height": {"width": "100%"},
        "interval": {"width": "100%"},
        "user": {"width": "100%"},
        "title": {"width": "100%"},
    }

    def build_main_layout(self):
        """图标专用布局"""
        return widgets.VBox(
            [
                widgets.HBox(
                    [
                        self.controls["shell"].widget,
                        self.controls["platform"].widget,
                    ],
                    layout=widgets.Layout(width="100%", grid_gap="0.5rem"),
                ),
                widgets.HBox(
                    [
                        self.controls["style"].widget,
                        self.controls["color"].widget,
                        self.controls["blink"].widget,
                    ],
                    layout=widgets.Layout(width="100%", grid_gap="0.5rem"),
                ),
                *[
                    widgets.HBox(
                        [self.controls[name].widget],
                        layout=widgets.Layout(width="100%", grid_gap="0.5rem"),
                    )
                    for name in [
                        "interval",
                        "height",
                        "user",
                        "title",
                    ]
                ],
                widgets.HBox(
                    [self.generate_btn, self.reset_btn],
                    layout=widgets.Layout(
                        width="100%", justify_content="flex-end", grid_gap="1rem"
                    ),
                ).add_class("preview-btn-group"),
                self.config_output,
            ],
            layout=widgets.Layout(width="100%", grid_gap="0.5rem"),
        )


if __name__ == "__main__":
    terminal_interactive = TerminalInteractive()
    terminal_interactive.run()
